package com.heima.admin.service.Imp;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dto.SensitiveDTO;
import com.heima.model.admin.pojo.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class SensitiveServiceImp extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {
    /**
     * 敏感词查询
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult sensitiveList(SensitiveDTO dto) {
        dto.checkParam();
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        //添加条件
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        //判断name是否存在
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.eq(AdSensitive::getSensitives,dto.getName());
        }
        //分页查询
        IPage<AdSensitive> sensitiveIPage = page(page, wrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal(), sensitiveIPage.getRecords());
    }

    /**
     * 敏感词新增
     * @param
     * @return
     */
    @Override
    public ResponseResult saveSensitive(SensitiveDTO dto) {
        String sensitive = dto.getSensitives();
        if (StringUtils.isBlank(sensitive)) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"数据不能为空");
        }
        //2. 判断是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, dto.getSensitives()));
        if(count > 0){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"敏感词不能相同!");
        }
        //将数据进行封装
        AdSensitive adSensitive = new AdSensitive();
        adSensitive.setSensitives(sensitive);
        adSensitive.setCreatedTime(new Date());
        //保存
        save(adSensitive);

        return ResponseResult.okResult();
    }

    /**
     * 修改
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive sensitive) {
        //数据校验
        if (StringUtils.isBlank(sensitive.getSensitives())) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"敏感词不能为空");
        }

        //2. 判断是否存在
        int count = this.count(Wrappers.
                <AdSensitive>lambdaQuery().
                eq(AdSensitive::getSensitives, sensitive.getSensitives()));
        if(count > 0){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"敏感词不能相同!");
        }

        updateById(sensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //数据校验
        if (id == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能为空!");
        }
        removeById(id);
        return ResponseResult.okResult();
    }
        @Autowired
        private SensitiveMapper sensitiveMapper;
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(sensitiveMapper.findAllSensitives());
    }
}
